package com.qiaofang.collectmanage.dao.impl;

import com.qiaofang.collectmanage.constants.ElementRuleFieldEnum;
import com.qiaofang.collectmanage.dao.ElementRuleDao;
import com.qiaofang.collectmanage.dao.domain.ElementRule;
import com.qiaofang.collectmanage.dao.mapper.ElementRuleMapper;
import com.qiaofang.collectmanage.stub.bean.SearchElementRulePostDTO;
import com.qiaofang.collectmanage.stub.constants.DeletedEnum;
import com.qiaofang.common.datasource.tkmapper.InsertListSelectiveMapperUtil;
import com.qiaofang.common.exception.BusinessException;
import com.qiaofang.common.model.page.PageDTO;
import com.qiaofang.common.page.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Repository("elementRuleDao")
public class ElementRuleDaoImpl implements ElementRuleDao {
    @Autowired
    ElementRuleMapper elementRuleMapper;
    @Override
    public ElementRule getElementRuleById(Integer id) {
        ElementRule elementRule = new ElementRule();
        elementRule.setId(id);
        return elementRuleMapper.selectOne(elementRule);
    }

    @Override
    public int updateElementRule(ElementRule elementRule) {
        Example ex = new Example(ElementRule.class);
        ex.createCriteria()
                .andIsNotNull("id")
                .andEqualTo("id", elementRule.getId());
        return elementRuleMapper.updateByExampleSelective(elementRule, ex);
    }

    @Override
    public int removeElementRule(Integer id) {
        Example ex = new Example(ElementRule.class);
        ex.createCriteria().andEqualTo("id", id);
        ElementRule elementRule = new ElementRule();
        elementRule.setDeleted(DeletedEnum.YES.getValue());
        elementRule.setUpdatedTime(new Date());
        return elementRuleMapper.updateByExampleSelective(elementRule, ex);
    }

    @Override
    public List<ElementRule> listElementRule(ElementRule elementRule) {
        if (elementRule == null) {
            elementRule = new ElementRule();
        }
        return elementRuleMapper.select(elementRule);
    }

    @Override
    public void saveElementRule(ElementRule elementRule) {
        int i = elementRuleMapper.insertSelective(elementRule);
        if (i == 0) {
            throw new BusinessException("新增元素规则失败");
        }
    }

    @Override
    public ElementRule getElementRule(ElementRule collecRule) {
        return elementRuleMapper.selectOne(collecRule);
    }


    @Override
    public List<ElementRule> searchElementRulePage(SearchElementRulePostDTO dto, PageDTO page) {
        Example example = new Example(ElementRule.class);
        page.setSortBy("created_time");
        page.setDesc("desc");
        String orderBy = page.getSortBy()+" "+ page.getDesc();
        example.setOrderByClause(orderBy);

        example.createCriteria()
                .andEqualTo(ElementRuleFieldEnum.COLLECT_RULE_UUID.getValue(), dto.getCollectRuleUuid())
                .andEqualTo(ElementRuleFieldEnum.DEPTH.getValue(), dto.getDepth())
                .andEqualTo(ElementRuleFieldEnum.DEPTH_DESC.getValue(), dto.getDepthDesc())
                .andEqualTo(ElementRuleFieldEnum.FIELD_NAME.getValue(), dto.getFieldName())
                .andEqualTo(ElementRuleFieldEnum.RETURN_ELEMENT.getValue(), dto.getReturnElement())
                .andEqualTo(ElementRuleFieldEnum.RETURN_ELEMENT_VALUE.getValue(), dto.getReturnElementValue())
                .andEqualTo(ElementRuleFieldEnum.RETURN_VALUE.getValue(), dto.getReturnValue())
                .andEqualTo(ElementRuleFieldEnum.DELETED.getValue(),DeletedEnum.NO.getValue());

        return elementRuleMapper.selectByExampleAndRowBounds(example, PageUtil.getRowBounds(page));
    }

    @Override
    public Long countSearchElementRulePage(SearchElementRulePostDTO dto) {
        Example example = new Example(ElementRule.class);
        example.createCriteria()
                .andEqualTo("collectRuleUuid", dto.getCollectRuleUuid())
                .andEqualTo("depth", dto.getDepth())
                .andEqualTo("depthDesc", dto.getDepthDesc())
                .andEqualTo("fieldName", dto.getFieldName())
                .andEqualTo("returnElement", dto.getReturnElement())
                .andEqualTo("returnElementValue", dto.getReturnElementValue())
                .andEqualTo("returnValue", dto.getReturnValue())
                .andEqualTo("deleted",DeletedEnum.NO.getValue());

        return Long.valueOf(elementRuleMapper.selectCountByExample(example));
    }

    @Override
    public List<ElementRule> getElementRuleByUUid(String collectRuleUuid) {
        Example ex = new Example(ElementRule.class);
        ex.createCriteria().andEqualTo("collectRuleUuid", collectRuleUuid);
        return elementRuleMapper.selectByExample(ex);
    }

    @Override
    public int saveElementRuleBatch(List<ElementRule> elementRuleList) {
        return InsertListSelectiveMapperUtil.insertListSelective(elementRuleMapper, elementRuleList);
    }

    @Override
    public int maxDepth(ElementRule elementRule) {
        return elementRuleMapper.maxDepth(elementRule);
    }



    @Override
    public List<Integer> findDepthsByUuid(String uuid) {
        List<Integer> depths = new ArrayList<Integer>();
        depths = elementRuleMapper.findDepthsByUuid(uuid);
        return depths;
    }

    @Override
    public List<ElementRule> findByUuidAndDepth(String uuid, Integer depth) {
        Example ex = new Example(ElementRule.class);
        ex.createCriteria().andEqualTo("collectRuleUuid", uuid).andEqualTo("depth",String.valueOf(depth));
        return elementRuleMapper.selectByExample(ex);
    }


}
